import Vue from 'vue'
import Router from 'vue-router'

import 'nprogress/nprogress.css'
// 审批
import approvalsRouter from '@/router/modules/approvals'
// 考勤
import attendancesRouter from '@/router/modules/attendances'
// 组织架构
import departmentsRouter from '@/router/modules/departments'
// 角色设置
import settingRouter from '@/router/modules/setting'
// 员工管理
import employeesRouter from '@/router/modules/employees'
// 权限管理
import permissionRouter from '@/router/modules/permission'
// 工资
import salarysRouter from '@/router/modules/salarys'
// 社保
import socialRouter from '@/router/modules/social'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    // 组件懒加载
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    hidden: true,
    path: '/input',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: '',
      name: 'input',
      component: () => import('@/views/input/index'),
      meta: { title: '导入', icon: '' }
    }]
  }
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]
// 有权限的动态路由 asyncRoutes
export const asyncRoutes = [
  // 组织架构
  departmentsRouter,
  // 角色设置
  settingRouter,
  // 权限管理
  permissionRouter,
  // 审批
  approvalsRouter,
  // 考勤
  attendancesRouter,
  // 员工
  employeesRouter,
  // 工资
  salarysRouter,
  // 社保
  socialRouter
]
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    ...constantRoutes,
    // ...asyncRoutes,
    { path: '*', redirect: '/404', hidden: true }
  ]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
